package com.bootdo.system.service.impl;

import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.bootdo.system.dao.ConfirmImgDao;
import com.bootdo.system.domain.ConfirmImgDO;
import com.bootdo.system.service.ConfirmImgService;
import org.springframework.web.multipart.MultipartFile;


@Service
public class ConfirmImgServiceImpl implements ConfirmImgService {
	private Logger logger = LoggerFactory.getLogger(ConfirmImgServiceImpl.class);

	@Autowired
	private ConfirmImgDao confirmImgDao;

	@Value("${bootdo.uploadPath}")
	private String uploadPath;
	
	@Override
	public ConfirmImgDO get(String id){
		return confirmImgDao.get(id);
	}
	
	@Override
	public List<ConfirmImgDO> list(Map<String, Object> map){
		return confirmImgDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return confirmImgDao.count(map);
	}
	
	@Override
	public int save(MultipartFile[] files) throws Exception{
		for(int i=0;i<files.length; i++){
			String id = UUID.randomUUID().toString().replaceAll("-","");
			//上传图片
			String imgUrl = FileUtil.upload(files[i], uploadPath, id);

			//文件名应是传递单号
			String fileName = files[i].getOriginalFilename();
			fileName = fileName.substring(0, fileName.lastIndexOf("."));
			//查询是否上传过 该传递单号的回执单
			List<ConfirmImgDO> reportImg = confirmImgDao.getImgInfo(fileName);
			if(!reportImg.isEmpty()){
				//上传过了先删除
				confirmImgDao.remove(reportImg.get(0).getId());
			}
			ConfirmImgDO confirmImgDO = new ConfirmImgDO();
			confirmImgDO.setId(id);
			confirmImgDO.setExpressNo(fileName);
			confirmImgDO.setImgUrl("/files" + imgUrl);
			confirmImgDO.setCreateTime(new Date());
			confirmImgDO.setCreateUser(ShiroUtils.getUserId().toString());
			confirmImgDO.setIsDelete(0);
			confirmImgDao.save(confirmImgDO);
		}
		return 1;
	}
	
	@Override
	public int update(ConfirmImgDO confirmImg){
		return confirmImgDao.update(confirmImg);
	}
	
	@Override
	public int remove(String id){
		return confirmImgDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return confirmImgDao.batchRemove(ids);
	}

	@Override
	public List<ConfirmImgDO> getImgInfo(String expressNo) {
		return confirmImgDao.getImgInfo(expressNo);
	}

	@Override
	public List<ConfirmImgDO> getImgList(String[] ids){
		return confirmImgDao.getImgList(ids);
	}



}
